using System.Drawing;
using System.Drawing.Imaging;

namespace FlickrShots.Framework.flickr
{
    public enum PhotoUrl
    {
        Small,
        Thumbnail,
        Square,
        Meduim,
        Large,
        Original
    }

	public class Photo
	{
        private FlickrNet.Photo _flickrPhoto;
        private Bitmap _image = null;

        public Photo(FlickrNet.Photo photo)
		{
            _flickrPhoto = photo;
		}

        public void Download()
        {
            Download(PhotoUrl.Original);    
        }

	    public void Download(PhotoUrl urlType)
	    {
	        switch (urlType)
	        {
                case PhotoUrl.Original:
	                DownloadImage(_flickrPhoto.OriginalUrl);
                    break;
	        }
	    }

        public string Title
        {
            get { return _flickrPhoto.Title; }
        }

        private void DownloadImage(string url)
        {
            _image = (Bitmap)Bitmap.FromStream(new Connectr().Flickr.DownloadPicture(url));
		    _image.Save(FileName, ImageFormat.Bmp);
        }

	    public string FileName
	    {
	        get
	        {
                string fileNameFormat = "{0}\\{1}.{2}";
                string photoFolder = Preferences.Instance.PhotoFolder;
                return string.Format(fileNameFormat, photoFolder, Title, ImageFormat.Bmp.ToString());
	        }
	    }

	    public bool IsDownloaded
	    {
	        get { return _image != null; }
	    }

//        public FlickrNet.Size[] Sizes
//        {
//            get
//            {
//                return new Connectr().Flickr.PhotosGetSizes(_flickrPhoto.PhotoId).SizeCollection;
//            }
//        }

//        public int Width
//        {
//            get
//            {
//                return LoadImageFromFile(FileName).Width;
//            }
//        }
//
//        public int Height
//        {
//            get 
//            {
//                return LoadImageFromFile(FileName).Height;
//            }
//        }
//
//        private Bitmap LoadImageFromFile(string fileName)
//        {
//            if (_image == null) 
//            {
//                _image = (Bitmap)Bitmap.FromFile(fileName);
//            }
//            return _image;
//        }

	}
}
